package com.logic.modular.gameCore.season.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comment.exception.ServiceException;
import com.comment.util.pay.LevelMapping;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.logic.modular.gameCore.season.info.domain.CoreSeasonAward;
import com.logic.modular.gameCore.season.info.mapper.CoreSeasonAwardMapper;
import com.logic.modular.gameCore.season.info.service.CoreSeasonAwardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (CoreSeasonAward)表服务实现类
 *
 * @author cxy
 * @since 2024-12-30 11:03:03
 */
@Slf4j
@Service
public class CoreSeasonAwardServiceImpl extends ServiceImpl<CoreSeasonAwardMapper, CoreSeasonAward> implements
        CoreSeasonAwardService {
    private static final Cache<String, Map<Long, CoreSeasonAward>> cache = Caffeine.newBuilder().build();
    private static LevelMapping levelMapping;

    @PostConstruct
    private void init() {
        CoreSeasonAward initSelect = CoreSeasonAward.builder().build();
        List<CoreSeasonAward> list = this.list(new LambdaQueryWrapper<>(initSelect));
        cache.put(
                "coreSeasonAward",
                list.stream().collect(Collectors.toMap(CoreSeasonAward::getId, CoreSeasonAward::init))
        );
        log.info("战令奖励数据缓存完成,数据量为:{}", list.size());

        long lastLeveTotal = 0;
        levelMapping = new LevelMapping();
        for (CoreSeasonAward nextLeve : list) {
            Long totalExp = nextLeve.getTotalExp();
            long nextNeed = totalExp - lastLeveTotal;

            LevelMapping.LevelUp levelUp = new LevelMapping.LevelUp();
            levelUp.setTargetLevel(nextLeve.getLevel());
            levelUp.setUpNeed(BigDecimal.valueOf(nextNeed));
            levelMapping.addPool(levelUp);

            lastLeveTotal = nextLeve.getTotalExp();
        }
    }

    public static List<CoreSeasonAward> listCache() {
        return new ArrayList<>(mapCache().values());
    }

    public static Map<Long, CoreSeasonAward> mapCache() {
        Map<Long, CoreSeasonAward> values = cache.getIfPresent("coreSeasonAward");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }

    public static CoreSeasonAward getCache(Long id) {
        return mapCache().get(id);
    }

    // 经验值获取等级
    public Integer getLevel(Long exp) {
        LambdaQueryWrapper<CoreSeasonAward> one = new LambdaQueryWrapper<CoreSeasonAward>()
                // 小于=
                .le(CoreSeasonAward::getTotalExp, exp)
                // 大于=
                .ge(CoreSeasonAward::getTotalExp, 0)
                // 最大的
                .orderByDesc(CoreSeasonAward::getTotalExp)
                .last("limit 1");
        CoreSeasonAward oneOpt = getOne(one);
        if (oneOpt == null) {
            throw new ServiceException("等级获取失败");
        }
        return oneOpt.getLevel();
    }

}
